﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NewsDiscoveryEngine;
using NewsMine.DataManagers;
using NewsMine.DomainObjects;
using System.Web;
using NewsDiscoveryEngine.Classification;
using System.Threading;

namespace WinApp.JobEngine.Hosting
{
    public partial class Menu_Identifier : Form
    {
        public Menu_Identifier()
        {
            InitializeComponent();
        }

        private void btnMenuTagsIdentify_Click(object sender, EventArgs e)
        {
            IdentifyTagsFromMenuUrls();
        }

        private void IdentifyAndSaveTagsForMenuItems()
        {
            // here we need to identify and also save the tags along with the menu item in the websiteInfo.
            // And also we need to save back to the websiteMaster database.

        }

        private void IdentifyTagsFromMenuUrls()
        {
            /*
             * Get all the menu urls from the website master db
             * 
             * for each url split the url into segments
             * Ignore the domain name segment.
             * if the there is only website domain in the menu, we need to consder that menu url as home / topstories / head lines
             * 
             * We need to add all the idenfied items to Tags property. So that we can use them in the indexing process. 
             * 
             * */

            WebSiteInfoManager webSitInfoManager = new WebSiteInfoManager();

            var menuItems = WebSiteInfoManager.GetAllMenuItems();

            Dictionary<string, int> tagCounts = new Dictionary<string, int>();

            foreach (WebSiteMenu menu in menuItems)
            {
                MergeTagCountFromMenuUrl(ref tagCounts, menu);
            }

            var sortedDict = (from entry in tagCounts orderby entry.Value descending select entry)
                .ToDictionary(pair => pair.Key, pair => pair.Value);


            MessageBox.Show(tagCounts.Count.ToString());

        }

        private void MergeTagCountFromMenuUrl(ref Dictionary<string, int> tagCounts, WebSiteMenu menu)
        {
            List<string> tagsFromUrl = UrlBasedTagsDetectionClassifier.GetTagsFromUrl(menu.Url);

            // merge the resulted tags in the tags count dictionary.

            foreach (string tag in tagsFromUrl)
            {
                if (tagCounts.Keys.Contains(tag) == true)
                {
                    //if the item is already exists in the dictionary, we need to just increase the count.
                    tagCounts[tag] = tagCounts[tag] + 1;
                }
                else
                {
                    //if the item is not exists, we need to just included
                    tagCounts.Add(tag, 1);
                }
            }
        }

        // Process the html feed into staging area.

        private void btnReprocessToStagingIndex_Click(object sender, EventArgs e)
        {
           
            MessageBox.Show("nothing in side.");
        }

      

        private void btnBrowserToPicture_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://www.google.com/");

            Thread.Sleep(5000);

            //getthe instance of the graphics from the control
            Graphics g = webBrowser1.CreateGraphics();

            //new bitmap object to save the image
            Bitmap bmp = new Bitmap(webBrowser1.Width, webBrowser1.Height);

            //Drawing control to the bitmap
            webBrowser1.DrawToBitmap(bmp, new Rectangle(0, 0, webBrowser1.Width, webBrowser1.Height));

            bmp.Save("c:\\browserImage.bmp");
            bmp.Dispose();
        }
    }
}
